home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / p-interp.lha / p-interp-0.5 / xturtleserver.c < prev   
C/C++ Source or Header  |  2001-05-23  |  13KB  |  548 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: xturtleserver.c,v $
  22.   Revision 1.1  2001/05/23 21:16:41  mario
  23.   Turtlegraphics wurde als eigener Prozess ausgelagert.
  24.  
  25.   Revision 1.2  2001/05/20 13:12:02  mario
  26.   CVS-Idents und Logs eingefügt
  27.  
  28.  
  29. */
  30.  
  31. #ident "$Id: xturtleserver.c,v 1.1 2001/05/23 21:16:41 mario Exp $";
  32.  
  33. #include <stdio.h>
  34. #include <math.h>
  35. #include <X11/Intrinsic.h>
  36. #include <X11/StringDefs.h>
  37. #include <X11/Core.h>
  38.  
  39. /*
  40. #include <X11/Xlib.h>
  41. #include <X11/Xutil.h>
  42. */
  43.  
  44.  
  45. #define XSIZE 280
  46. #define YSIZE 192
  47.  
  48. #define PENCOLOR_NONE        0
  49. #define PENCOLOR_WHITE        1
  50. #define PENCOLOR_BLACK        2
  51. #define PENCOLOR_REVERSE    3
  52. #define PENCOLOR_RADAR        4
  53. #define PENCOLOR_BLACK1        5
  54. #define PENCOLOR_GREEN        6
  55. #define PENCOLOR_VIOLET        7
  56. #define PENCOLOR_WHITE1        8
  57. #define PENCOLOR_BLACK2        9
  58. #define PENCOLOR_ORANGE        10
  59. #define PENCOLOR_BLUE        11
  60. #define PENCOLOR_WHITE2        12
  61.  
  62. #define NUMBER(a)    (sizeof(a)/sizeof(a[0]))
  63.  
  64. static const char    *ColorNames[]={NULL, "white", "black", NULL,  NULL, 
  65.                        "black", "green",  "violet", "white",
  66.                        "black", "orange", "blue",   "white"};
  67.  
  68. static GC        ColorGCs[NUMBER(ColorNames)];
  69.  
  70. static struct
  71. {
  72.   int    xMin;
  73.   int    xMax;
  74.   int    yMin;
  75.   int    yMax;
  76. } CurrentViewPort;
  77. static int    CurrentPenColor;
  78. static int    CurrentTurtleX;
  79. static int    CurrentTurtleY;
  80. static int    CurrentTurtleAng;
  81. static int    CurrentCharType;
  82.  
  83. static unsigned char    *Buffer  = NULL;
  84.  
  85. XtAppContext    App;
  86. Widget        Top;
  87. Widget        w;
  88.  
  89. static void UpdatePixel(int x, int y)
  90. {
  91.   int Pixel=Buffer[y*280/2+x/2];
  92.   if (x&1)
  93.     Pixel >>= 4;
  94.   else
  95.     Pixel &= 0x0f;
  96.  
  97.   if (Pixel<NUMBER(ColorGCs) &&
  98.       ColorGCs[Pixel])
  99.     XFillRectangle(XtDisplay(w), XtWindow(w), ColorGCs[Pixel], x*2, (191-y)*2, 2, 2);
  100. }
  101.  
  102. static void UpdatePlane(int xmin, int xmax, int ymin, int ymax)
  103. {
  104.   int x, y;
  105.  
  106.   for (y=ymin; y<ymax; y++)
  107.     for (x=xmin; x<xmax; x++)
  108.       UpdatePixel(x, y);
  109. }
  110.  
  111. static int GetPixel(int x, int y)
  112. {
  113.   char ch=Buffer[y*280/2+x/2];
  114.  
  115.   if ((x&1))
  116.     return ((ch&0xf0)>>4);
  117.   else
  118.     return (ch&0x0f);
  119. }
  120.  
  121. static void SetPixel(int x, int y, int Color)
  122. {
  123.   char            *p=&Buffer[y*280/2+x/2];
  124.   static const char    Inverse[NUMBER(ColorNames)] =
  125.                 {0, 2, 1, 0, 0, 8, 7, 6, 5, 12, 11, 10, 9};
  126.  
  127.   if ( (x<CurrentViewPort.xMin) || (x>CurrentViewPort.xMax) ||
  128.        (y<CurrentViewPort.yMin) || (y>CurrentViewPort.yMax) )
  129.     return;
  130.  
  131.   if (Color == PENCOLOR_NONE)
  132.     return;
  133.   else if (Color == PENCOLOR_REVERSE)
  134.     Color=Inverse[GetPixel(x,y)];
  135.  
  136.   if ((x&1))
  137.     *p=(*p&0x0f)|(Color<<4);
  138.   else
  139.     *p=(*p&0xf0)|Color;
  140.   UpdatePixel(x, y);
  141. }
  142.  
  143. static void Line(int len, int ticks, int *LenVec, int *TickVec)
  144. {
  145.   int    i=len/2;
  146.   int    j;
  147.  
  148.   for (j=0;j<len; j++)
  149.     {
  150.       CurrentTurtleX+=LenVec[0];
  151.       CurrentTurtleY+=LenVec[1];
  152.       i+=ticks;
  153.       if (i>=len)
  154.     {
  155.       i-=len;
  156.       CurrentTurtleX+=TickVec[0];
  157.       CurrentTurtleY+=TickVec[1];
  158.     }
  159.       SetPixel(CurrentTurtleX, CurrentTurtleY, CurrentPenColor);
  160.     }
  161. }
  162.  
  163. static void MoveTo(int x, int y)
  164. {
  165.   static int right[] ={ 1,  0};
  166.   static int left[]  ={-1,  0};
  167.   static int up[]    ={ 0,  1};
  168.   static int down[]  ={ 0, -1};
  169.  
  170.   if (XtDisplay(w) && (CurrentPenColor!=PENCOLOR_NONE) )
  171.     {
  172.       int dx=x-CurrentTurtleX;
  173.       int dy=y-CurrentTurtleY;
  174.       if ( (dx>0) && (abs(dy)<=dx) )
  175.     Line(dx, abs(dy), right, (dy>0)?up:down);
  176.       else if ( (dx<0) && (abs(dy)<=-dx) )
  177.     Line(-dx, abs(dy), left, (dy>0)?up:down);
  178.       else if ( (dy>0) && (abs(dx<=dy)) )
  179.     Line(dy, abs(dx), up, (dx>0)?right:left);
  180.       else
  181.     Line(-dy, abs(dx), down, (dx>0)?right:left);
  182.     }
  183.   CurrentTurtleX=x;
  184.   CurrentTurtleY=y;
  185. }
  186.  
  187. void TurnTo(int Angle)
  188. {
  189.   CurrentTurtleAng=Angle;
  190.   while (CurrentTurtleAng<0)
  191.     CurrentTurtleAng+=360;
  192.   while (CurrentTurtleAng>=360)
  193.     CurrentTurtleAng-=360;
  194. }
  195.  
  196. static void FillScreen(int Color)
  197. {
  198.   int    x, y;
  199.   
  200.   for (y=CurrentViewPort.yMin; y<=CurrentViewPort.yMax; y++)
  201.     for (x=CurrentViewPort.xMin; x<=CurrentViewPort.xMax; x++)
  202.       SetPixel(x, y, Color);
  203. }
  204.  
  205. static void DrawByte(char Data, int XSkip, int Width,
  206.              int XScreen, int YScreen, int Mode)
  207. {
  208.   static const int    ModeTable[16][2]={
  209.     {PENCOLOR_BLACK, PENCOLOR_BLACK},    {PENCOLOR_REVERSE, PENCOLOR_BLACK},
  210.     {PENCOLOR_BLACK, PENCOLOR_REVERSE}, {PENCOLOR_REVERSE, PENCOLOR_REVERSE},
  211.     {PENCOLOR_NONE,  PENCOLOR_BLACK},   {PENCOLOR_WHITE,   PENCOLOR_BLACK},
  212.     {PENCOLOR_NONE,  PENCOLOR_REVERSE}, {PENCOLOR_WHITE,   PENCOLOR_REVERSE},
  213.     {PENCOLOR_BLACK, PENCOLOR_NONE},    {PENCOLOR_REVERSE, PENCOLOR_NONE},
  214.     {PENCOLOR_BLACK, PENCOLOR_WHITE},    {PENCOLOR_REVERSE, PENCOLOR_WHITE},
  215.     {PENCOLOR_NONE,  PENCOLOR_NONE},    {PENCOLOR_WHITE,   PENCOLOR_NONE},
  216.     {PENCOLOR_NONE,  PENCOLOR_WHITE},    {PENCOLOR_WHITE,   PENCOLOR_WHITE}   };
  217.   int i;
  218.  
  219.   for (i=0;i<Width; i++)
  220.     {
  221.       int Pixel;
  222.       if ( (i+XSkip) > 7 )
  223.     break;
  224.       Pixel = Data & (1 << (i+XSkip) ) ? 1:0;
  225.       SetPixel(XScreen+i, YScreen, ModeTable[Mode][Pixel]);
  226.     }
  227. }
  228.  
  229. static void WChar(unsigned char ch)
  230. {
  231.   int    y;
  232.   static const char    SystemCharset[256*8]={
  233. #ifndef MAKEDEPEND
  234. #include "system.charset.h"
  235. #endif
  236.   };
  237.  
  238.   for (y=0; y<8; y++)
  239.     DrawByte(SystemCharset[ch*8+y], 0, 7,
  240.          CurrentTurtleX, CurrentTurtleY+y, CurrentCharType);
  241.   CurrentTurtleX += 7;
  242. }
  243.  
  244. static void TurtleExposeHandler(Widget w, XtPointer Closure, 
  245.                 XEvent *Event, Boolean *Continue)
  246. {
  247.   UpdatePlane(0, 279,0,191);
  248. }
  249.  
  250. static void InitTurtle(int Width, int Height)
  251. {
  252.   Arg            args[5];
  253.   Cardinal        n;
  254.  
  255.   int            screen;
  256.   XSetWindowAttributes    set_win_attr;
  257.   long            win_attr_mask;
  258.   XSizeHints        the_size_hints;
  259.   XGCValues        GcValues;
  260.   int            i;
  261.  
  262.   if (!w)
  263.     {
  264.       if (!(Buffer=(char*)malloc(Width*Height/2)))
  265.     {
  266.       fprintf(stderr,"malloc() failed\n");
  267.       exit(1);
  268.     }
  269.       memset(Buffer, PENCOLOR_BLACK*0x11, Width*Height/2);
  270.  
  271.       n=0;
  272.       XtSetArg(args[n], XtNwidth, 2*Width); n++;
  273.       XtSetArg(args[n], XtNheight, 2*Height); n++;
  274.  
  275.  
  276.       w=XtCreateManagedWidget( "turtlegraphics", coreWidgetClass, Top, 
  277.                    args, n);
  278.  
  279.       XtAddEventHandler(w, ExposureMask, TRUE, TurtleExposeHandler, NULL);
  280.       XtRealizeWidget(Top);
  281.  
  282.       for (i=0; i<NUMBER(ColorNames); i++)
  283.     if (ColorNames[i])
  284.       {
  285.         XColor    color,color1;
  286.         if (!XAllocNamedColor(XtDisplay(w),
  287.                   DefaultColormap(XtDisplay(w),
  288.                           DefaultScreen(XtDisplay(w))),
  289.                   ColorNames[i], &color, &color1))
  290.           {
  291.         fprintf(stderr,"Unable to allocate color %s\n", 
  292.             ColorNames[i]);
  293.         if (strcmp(ColorNames[i],"black")==0)
  294.           GcValues.foreground=BlackPixel(XtDisplay(w),
  295.                          DefaultScreen(XtDisplay(w)));
  296.         else
  297.           GcValues.foreground=WhitePixel(XtDisplay(w),
  298.                          DefaultScreen(XtDisplay(w)));
  299.           }
  300.         else
  301.           GcValues.foreground = color.pixel;
  302.         ColorGCs[i]=XCreateGC( XtDisplay(w), XtWindow(w), GCForeground , &GcValues );
  303.       }
  304.     else
  305.       ColorGCs[i]=0;
  306.  
  307.     }
  308. #ifdef XXX
  309.   screen = DefaultScreen( XtDisplay(w) );
  310.       
  311.       /* set windows attributes */
  312.       set_win_attr.border_pixel = WhitePixel( XtDisplay(w), screen );
  313.       set_win_attr.background_pixel = BlackPixel( XtDisplay(w), screen );
  314.       set_win_attr.override_redirect = 0;
  315.       set_win_attr.backing_store = WhenMapped;
  316.       win_attr_mask = CWBorderPixel | CWBackPixel |
  317.     CWOverrideRedirect | CWBackingStore;
  318.  
  319.       XtWindow(w) = XCreateXtWindow(W)( XtDisplay(w), 
  320.                   RootXtWindow(W)( XtDisplay(w), screen),
  321.                   0, 0, Width*2, Height*2, 
  322.                   0, DefaultDepth( XtDisplay(w), screen ),
  323.                   InputOutput,
  324.                   DefaultVisual( XtDisplay(w), screen ),
  325.                   win_attr_mask,
  326.                   &set_win_attr );
  327.  
  328.       /*
  329.        * Set size hints & Properties
  330.        */
  331.       the_size_hints.width = Width*2; 
  332.       the_size_hints.height = Height*2;
  333.       the_size_hints.max_width = Width*2;
  334.       the_size_hints.max_height = Height*2;
  335.       the_size_hints.min_width = Width*2;
  336.       the_size_hints.min_height = Height*2;
  337.       the_size_hints.flags = PSize | PMaxSize | PMinSize;
  338.       XSetStandardProperties( XtDisplay(w), XtWindow(w),
  339.                   "Turtlegraphics", "Turtlegraphics",
  340.                   None, 0, 0,  &the_size_hints );
  341.       
  342.       /* SelectInput( XtDisplay(w), window, ExposureMask | StructureNotifyMask ); */
  343.  
  344. #endif
  345.  
  346.   CurrentViewPort.xMin=0;
  347.   CurrentViewPort.xMax=Width-1;
  348.   CurrentViewPort.yMin=0;
  349.   CurrentViewPort.yMax=Height-1;
  350.   FillScreen(PENCOLOR_BLACK);
  351.   CurrentTurtleX=CurrentViewPort.xMax/2;
  352.   CurrentTurtleY=CurrentViewPort.yMax/2;
  353.   CurrentTurtleAng=0;
  354.   CurrentCharType=10;
  355.   CurrentPenColor=PENCOLOR_NONE;
  356. }
  357.  
  358. static char *BufEnd=NULL;
  359. static char *RdPtr=NULL;
  360.  
  361. int tgetchar(void)
  362. {
  363.   static char Buffer[4096];
  364.   if (RdPtr >= BufEnd)
  365.     {
  366.       int Len=read(0, Buffer, sizeof(Buffer));
  367.       if ( Len < 1 )
  368.     return(EOF);
  369.       BufEnd=Buffer+Len;
  370.       RdPtr=Buffer;
  371.     }
  372.   return *RdPtr++;
  373. }
  374.  
  375. void TurtleCommandHandler(XtPointer Closure, int *Source, XtInputId *Id)
  376. {
  377.   int    i;
  378.   int    ch;
  379.   char    Buffer[80];
  380.   char    *p;
  381.  
  382.   do
  383.     {
  384.       for (i=0,p=Buffer; i<79; i++,p++)
  385.     {
  386.       ch=tgetchar();
  387.       if (ch == EOF)
  388.         exit(0);
  389.       if (ch == '\n')
  390.         break;
  391.       *p=ch;
  392.     }
  393.       *p='\0';
  394.  
  395.       if (p=strchr(Buffer,' '))
  396.     *p++='\0';
  397.   
  398.       if (strcmp(Buffer, "INITTURTLE")==0)
  399.     InitTurtle(280,192);
  400.       else if (strcmp(Buffer, "TURN")==0)
  401.     {
  402.       if (sscanf( p, "%d", &i)==1)
  403.         TurnTo(CurrentTurtleAng+i);
  404.     }
  405.       else if (strcmp(Buffer, "TURNTO")==0)
  406.     {
  407.       if (sscanf( p, "%d", &i)==1)
  408.         TurnTo(i);
  409.     }
  410.       else if (strcmp(Buffer, "MOVE")==0)
  411.     {
  412.       if (sscanf( p, "%d", &i)==1)
  413.         MoveTo(CurrentTurtleX+rint(cos(CurrentTurtleAng*3.14/180)*i),
  414.            CurrentTurtleY+rint(sin(CurrentTurtleAng*3.14/180)*i));
  415.     }
  416.       else if (strcmp(Buffer, "MOVETO")==0)
  417.     {
  418.       int y;
  419.       int x;
  420.       if (sscanf( p, "%d %d", &x, &y)==2)
  421.         MoveTo(x, y);
  422.     }
  423.       else if (strcmp(Buffer, "PENCOLOR")==0)
  424.     {
  425.       if (sscanf( p, "%d", &i)==1)
  426.         CurrentPenColor=i;
  427.     }
  428.       else if (strcmp(Buffer, "TEXTMODE")==0)
  429.     {
  430.       if (w)
  431.         XtMapWidget(w);
  432.     }
  433.       else if (strcmp(Buffer, "GRAFMODE")==0)
  434.     {
  435.       if (w)
  436.         XtUnmapWidget(w);
  437.     }
  438.       else if (strcmp(Buffer, "FILLSCREEN")==0)
  439.     {
  440.       if (sscanf( p, "%d", &i)==1)
  441.         if (XtDisplay(w))
  442.           FillScreen(i);
  443.     }
  444.       else if (strcmp(Buffer, "VIEWPORT")==0)
  445.     {
  446.       int yMax;
  447.       int yMin;
  448.       int xMax;
  449.       int xMin;
  450.       if (sscanf(p, "%d %d %d %d", &xMin, &xMax, &yMin, &yMax)==4)
  451.         {
  452.           CurrentViewPort.yMax=yMax;
  453.           CurrentViewPort.yMin=yMin;
  454.           CurrentViewPort.xMax=xMax;
  455.           CurrentViewPort.xMin=xMin;
  456.         }
  457.     }
  458.       else if (strcmp(Buffer, "TURTLEX")==0)
  459.     {
  460.       printf("%d\n",CurrentTurtleX);
  461.       fflush(stdout);
  462.     }
  463.       else if (strcmp(Buffer, "TURTLEY")==0)
  464.     {
  465.       printf("%d\n",CurrentTurtleY);
  466.       fflush(stdout);
  467.     }
  468.       else if (strcmp(Buffer, "TURTLEANG")==0)
  469.     {
  470.       printf("%d\n",CurrentTurtleAng);
  471.       fflush(stdout);
  472.     }
  473.       else if (strcmp(Buffer, "SCREENBIT")==0)
  474.     {
  475.       int y;
  476.       int x;
  477.       if (sscanf(p, "%d %d",&x, &y)==2)
  478.         printf("%c\n",XtDisplay(w) && (GetPixel(x,y)!=PENCOLOR_BLACK)?'1':'0');
  479.       fflush(stdout);
  480.     }
  481.       else if (strcmp(Buffer, "DRAWBLOCK")==0)
  482.     {
  483.       int x;
  484.       int y;
  485.       int Mode;
  486.       int YScreen;
  487.       int XScreen;
  488.       int Height;
  489.       int Width;
  490.       int YSkip;
  491.       int XSkip;
  492.       int RowSize;
  493.       
  494.       if (sscanf( p, "%d %d %d %d %d %d %d %d", 
  495.               &RowSize, &XSkip, &YSkip,
  496.               &Width, &Height, &XScreen, &YScreen, &Mode)==8)
  497.         {
  498.           for (y=0; y<Height; y++)
  499.         {
  500.           int  Skip=XSkip&7;
  501.           for (x=0;x<Width;)
  502.             {
  503.               int  W=Width-x;
  504.               char Data=tgetchar();
  505.               
  506.               if ( W > 8-Skip)
  507.             W = 8-Skip;
  508.               
  509.               if (XtDisplay(w))
  510.             DrawByte(Data, Skip, W, XScreen+x, YScreen+y, Mode);
  511.               x+=W;
  512.               Skip=0;
  513.             }
  514.         }
  515.         }
  516.     }
  517.       else if (strcmp(Buffer, "WCHAR")==0)
  518.     {
  519.       if (sscanf(p, "%d",&i)==1)
  520.         if (XtDisplay(w))
  521.           WChar(i&0xff);
  522.     }
  523.       else if (strcmp(Buffer, "WSTRING")==0)
  524.     {
  525.       int  Len;
  526.       int  i;
  527.       if (sscanf(p, "%d",&Len)==1)
  528.         if (XtDisplay(w))
  529.           for (i=0;i<Len; i++)
  530.         WChar(tgetchar());
  531.     }
  532.       else if (strcmp(Buffer, "CHARTYPE")==0)
  533.     {
  534.       if (sscanf(p, "%d", &i)==1)
  535.         CurrentCharType=i&0x0f;
  536.     }
  537.     }  while (RdPtr < BufEnd);
  538. }
  539.  
  540. int main(int argc, char *argv[])
  541. {
  542.   Top=XtAppInitialize(&App, "XTurtlegraphicsServer", NULL, 0,
  543.               &argc, argv, NULL, NULL, 0);
  544.   XtAppAddInput(App, fileno(stdin), (XtPointer)XtInputReadMask,
  545.         TurtleCommandHandler, NULL);
  546.   XtAppMainLoop(App);
  547. }
  548.